#!/usr/bin/env python
#
# Jisko Python Library: to interact with the Jisko API
# Copyright (C) 2008 Carlos Hellin <charlie@charli.es>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import base64
import urllib
import urllib2

# We require the simplejson library: http://pypi.python.org/pypi/simplejson
import simplejson
parse_json = lambda s: simplejson.loads(s.decode("utf-8"))

class Jisko(object):
    def __init__(self, auth_nickname, auth_key):
        "Creates a new Jisko session for the given user."
        self.auth_nickname = auth_nickname
        self.auth_key = auth_key

    def fetch_archive(self, **kwargs):
        "Get the notes of the identified user."
        return self._fetch("/getFriends", **kwargs)

    def fetch_public(self, **kwargs):
        "Get the notes from the public timeline."
        return self._fetch("/getPublic", **kwargs)

    def fetch_friends(self, **kwargs):
        "Get the timeline of persons who follows the identified user."
        return self._fetch("/getFriends", **kwargs)

    def fetch_private(self, **kwargs):
        "Get the private notes of the identified user."
        return self._fetch("/getPrivate", **kwargs)

    def fetch_replies(self, **kwargs):
        "Get the replies of the user of the identified user."
        return self._fetch("/getReplies", **kwargs)

    def fetch_favorites(self, **kwargs):
        "Get the notes marked as favorites by the identified user."
        return self._fetch("/getFavorites", **kwargs)

    def update_status(self, note, twitter=0, **kwargs):
        "Update the status by sending a note. To ship to Twitter, use twitter=1"
        args = {
            "note": note,
            "twitter": twitter
        }
        result = self._fetch("/updateStatus", args)
        return result

    def _fetch(self, uri, post_args=None, **url_args):
        args = urllib.urlencode(url_args)
        url = "http://jisko.net/api" + uri + "?format=json" + args
        if post_args is not None:
            request = urllib2.Request(url, urllib.urlencode(post_args))
        else:
            request = urllib2.Request(url)
        if self.auth_nickname and self.auth_key:
            pair = "%s:%s" % (self.auth_nickname, self.auth_key)
            token = base64.b64encode(pair)
            request.add_header("Authorization", "Basic %s" % token)
        stream = urllib2.urlopen(request)
        data = stream.read()
        stream.close()
        return parse_json(data)
